package com.yl.auth.mapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yl.auth.entity.Jigouzhongxinduanaccount;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author L
 * @since 2023-07-20
 */
public interface JigouzhongxinduanaccountMapper extends BaseMapper<Jigouzhongxinduanaccount> {

    @Insert(" insert into role_account(id,role_id,account_id) values(0,#{roleId},#{id})")
    void saveAccountRole(@Param("id") Long id,@Param("roleId") Long roleId);

    Page<Jigouzhongxinduanaccount> findAllAndPage(Page<Jigouzhongxinduanaccount> page1);

    @Select("select r.id from jigouzhongxinduanaccount a\n" +
            "            left join role_account ra on a.id = ra.account_id\n" +
            "            left join role r on ra.role_id = r.id\n" +
            "            where a.id = #{id}")
    List<Long> getRoleByAccount(@Param("id") Long id);

    @Delete("delete from role_account where account_id = #{id}")
    void deleteByRoleId(@Param("id") Long id);

    @Select("select b.role_id from jigouzhongxinduanaccount a \n" +
            "left join role_account b on a.id = b.account_id\n" +
            "left join jigouzhongxinduanrole c on b.role_id = c.id\n" +
            "where a.id = #{id}")
    String getRoleName(@Param("id") Long id);
}
